package com.enba.ali.pay.config;


import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.kernel.Config;
import lombok.Data;
import lombok.Setter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;


@Configuration
@ConfigurationProperties(prefix = "alipay")
@Data
public class AliPaymentConfig {

  Logger log = LoggerFactory.getLogger(AliPaymentConfig.class);
  private String protocol;
  private String gatewayUrl;
  private String signType;

  private String appId;

  private String merchantPrivateKey;

  private String alipayPublicKey;

  private String notifyUrl;

  private String charset;

  private String encryptKey;

  private Config configs;


  @PostConstruct
  public void init() {

    // 设置全局配置（全局只需设置一次）
    log.info("======= 支付宝SDK初始化成功 =======");
  }

  @Bean
  public AlipayClient alipayClient() {
    log.info("初始化支付宝支付相关配置开始");
    AlipayConfig alipayConfig = new AlipayConfig();
    alipayConfig.setServerUrl(gatewayUrl);
    alipayConfig.setAppId(appId);
    alipayConfig.setPrivateKey(merchantPrivateKey);
    alipayConfig.setFormat("json");
    alipayConfig.setAlipayPublicKey(alipayPublicKey);
    alipayConfig.setCharset(charset);
    alipayConfig.setSignType(signType);
    try {
      return new DefaultAlipayClient(alipayConfig);
    } catch (AlipayApiException e) {
      log.debug("初始化支付宝支付相关配置失败");
    }
    return null;
  }
}
